﻿using System.Data.SQLite;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Cashier
{
    /// <summary>
    /// WaitMenuWindow.xaml 的交互逻辑
    /// </summary>
    public partial class WaitMenuWindow : Window
    {
        private int table_id;
        public WaitMenuWindow()
        {
            InitializeComponent();
            ShowMenu();
        }

        public int Table_id { get => table_id; set => table_id = value; }
        public class Food
        {
            //private string test;
            //public string Test { get { return test; } set { test = value; } }
            public string food_id { get; set; }
            public string name { get; set; }
            public string price { get; set; }
            public string category { get; set; }
            public string number { get; set; }

        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            GetFoodListAndInsert();
        }
        private void ShowMenu()
        {
            try
            {
                //获取表格
                DataTable data = new DataTable("food");
                //DataColumn food_id = new DataColumn("food_id");
                data.Columns.Add(new DataColumn("food_id", typeof(string)));
                data.Columns.Add(new DataColumn("name", typeof(string)));
                data.Columns.Add(new DataColumn("price", typeof(string)));
                data.Columns.Add(new DataColumn("category", typeof(string)));

                using (SQLiteConnection conn = new SQLiteConnection(CommonValue.sqliteConnectString))
                {
                    conn.Open();
                    string cmd = "select * from food";
                    using (SQLiteCommand myCmd = new SQLiteCommand(cmd, conn))
                    {
                        //SQLiteDataAdapter mda = new SQLiteDataAdapter(cmd, conn);
                        using (SQLiteDataReader reader = myCmd.ExecuteReader())
                        {
                            ObservableCollection<Food> memberData = new ObservableCollection<Food>();
                            while (reader.Read())
                            {

                                string food_id = reader["food_id"].ToString();
                                string name = reader["name"].ToString();
                                string price = reader["price"].ToString();
                                string category = reader["category"].ToString();


                                memberData.Add(new Food()
                                {
                                    food_id = food_id,
                                    name = name,
                                    price = price,
                                    category = category,
                                    number = "1"
                                });

                            }
                            dataGrid.DataContext = memberData;
                            conn.Close();
                        }
                    }
                }

            }
            catch (Exception e)
            {
                MessageBox.Show("查询菜单失败:" + e.Message);
            }

        }
        private int GetOderId()
        {
            Random random = new Random();
            int oder_id = random.Next(0, 5000);
            return oder_id;
        }
        private void GetFoodListAndInsert()
        {
            int oder_id;
            float sum = 0;//订单总价
            oder_id = GetOderId();
            List<Food> foodList = new List<Food>();

            foreach (Food fooTemp in dataGrid.SelectedItems)
            {

                // 测试

                string num = fooTemp.number;
                if (!num.Equals(""))
                {
                    foodList.Add(new Food()
                    {
                        food_id = fooTemp.food_id,
                        name = fooTemp.name,
                        price = fooTemp.price,
                        category = fooTemp.category,
                        number = fooTemp.number
                    });
                }
            }

            try
            {

                using (SQLiteConnection conn = new SQLiteConnection(CommonValue.sqliteConnectString))
                {
                    conn.Open();
                    foreach (Food foodTemp in foodList)
                    {
                        float total_price = float.Parse(foodTemp.price) * float.Parse(foodTemp.number);
                        sum += total_price;
                        String cmd = "insert into food_odered( food_id, food_name, oder_id, number, total_price, price) values('" + foodTemp.food_id + "','" + foodTemp.name + "','" + oder_id + "','" + foodList.Count + "','" + total_price + "','" + foodTemp.price + "')";
                        using (SQLiteCommand mycmd = new SQLiteCommand(cmd, conn))
                        {
                            mycmd.ExecuteNonQuery();
                        }
                    }

                    String cmd2 = "insert into oder( oder_id, sum,table_id) values('" + oder_id + "','" + sum + "'," + table_id + ")";
                    using (SQLiteCommand mycmd2 = new SQLiteCommand(cmd2, conn))
                    {

                        if (mycmd2.ExecuteNonQuery() > 0)
                        {
                            MessageBox.Show("下单成功！");
                            conn.Close();

                        }
                    }
                }
            }



            catch (Exception e)
            {
                MessageBox.Show("对不起，数据库连接失败，请再次尝试！" + e.Message);
            }


        }
    }
}
